/*
 * Copyright (c) 2016. For Intelligent Group.
 */
package com.intelligent.ispc.r.analysis.spc;

import com.google.common.collect.Lists;
import com.intelligent.ispc.core.dto.SpcConfigDto;
import com.intelligent.ispc.r.dto.RuleResultDto;
import com.intelligent.ispc.utils.ControlRuleConfigUtil;
import com.intelligent.ispc.utils.JudgeRuleType;

import java.util.List;

/**
 * Created by Peter on 2015/12/11 .
 * Copy it from ispc_web tp ispc_desktop by Eligi on 8/31.
 */
public class CConfiguration {

    private List<SpcConfigDto> controlWarnList = null;

    private double[] data;

    /**
     * constructor
     * @param controlWarnList warning rules
     * */
    public CConfiguration(List<SpcConfigDto> controlWarnList) {
        this.controlWarnList = controlWarnList;
    }

    /**
     * constructor
     * @param avgValue average value
     * @param stdevValue standard deviation value
     * @return result after input checked by kinds of rules
     * */
    public List<RuleResultDto> getRuleList(double avgValue, double stdevValue) {
        List<RuleResultDto> ruleResultDtoList = Lists.newArrayList();
        if (controlWarnList != null) {
            ControlRuleConfigUtil controlRuleConfigUtil = new ControlRuleConfigUtil();
            controlRuleConfigUtil.setAnalysData(data);
            for (SpcConfigDto obj : controlWarnList) {
                if ((obj == null)) {
                    continue;
                }

                JudgeRuleType iRuleType = JudgeRuleType.getByCode(obj.getName());
                if (iRuleType != null) {
                    RuleResultDto tempObj = null;
                    String[] ruleValueStr = obj.getValue().split(",");
                    int iSigma = Integer.parseInt(ruleValueStr[2].trim());
                    int iPoints = Integer.parseInt(ruleValueStr[0].trim());
                    int iSomePoints = Integer.parseInt(ruleValueStr[1].trim());
                    //the value of ifCheck is 0 means false , is 1 means true
                    int ifCheck = Integer.parseInt(ruleValueStr[3].trim());
                    if (ifCheck == 0) {
                        continue;
                    }

                    switch (iRuleType) {
                        case R1:
                            tempObj = controlRuleConfigUtil.setRuleR1(iPoints, iSigma, avgValue, stdevValue);
                            break;
                        case R2:
                            tempObj = controlRuleConfigUtil.setRuleR2(iPoints, avgValue);
                            break;
                        case R3:
                            tempObj = controlRuleConfigUtil.setRuleR3(iPoints);
                            break;
                        case R4:
                            tempObj = controlRuleConfigUtil.setRuleR4(iPoints);
                            break;
                        case R5:
                            tempObj = controlRuleConfigUtil.setRuleR5And6(iPoints, iSomePoints, avgValue, stdevValue, iSigma, JudgeRuleType.R5.getCode());
                            break;
                        case R6:
                            tempObj = controlRuleConfigUtil.setRuleR5And6(iPoints, iSomePoints, avgValue, stdevValue, iSigma, JudgeRuleType.R6.getCode());
                            break;
                        case R7:
                            tempObj = controlRuleConfigUtil.setRuleR7And8(iPoints, avgValue, stdevValue, iSigma, JudgeRuleType.R7.getCode());
                            break;
                        case R8:
                            tempObj = controlRuleConfigUtil.setRuleR7And8(iPoints, avgValue, stdevValue, iSigma, JudgeRuleType.R8.getCode());
                            break;
                        default:
                            break;
                    }
                    if (tempObj != null) {
                        ruleResultDtoList.add(tempObj);
                    }
                }
            }
        }
        return ruleResultDtoList;
    }

    public void setControlWarnList(List<SpcConfigDto> controlWarnList) {
        this.controlWarnList = controlWarnList;
    }

    public double[] getData() {
        return data;
    }

    public void setData(double[] data) {
        this.data = data;
    }
}
